//
//  PlayerDetailsViewController.swift
//  Ratings
//
//  Created by cygarm on 15/7/18.
//  Copyright (c) 2015年 cygarm. All rights reserved.
//

import UIKit

class PlayerDetailsViewController: UITableViewController {
  var player: Player!
  var game: String = "Chess"
  
  @IBOutlet weak var nameTextField: UITextField!
  @IBOutlet weak var detailLabel: UILabel!
  @IBAction func selectedGame(segue: UIStoryboardSegue) {
    if let gamePickerViewController = segue.sourceViewController as? GamePickerViewController, selectedGame = gamePickerViewController.selectedGame {
      detailLabel.text = selectedGame
      game = selectedGame
    }
  }

    override func viewDidLoad() {
        super.viewDidLoad()
        detailLabel.text = game

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source
    /*
    When you use static cells, your table view controller doesn’t need a data source. Because you used an Xcode template to create the PlayerDetailsViewController class, it still has some placeholder code for the data source and that will prevent the static cells from working properly – that’s why your static content wasn’t visible here.
    */
  
  
  /*
  This just says that if the user tapped the first cell, the app should activate the text field. There is only one cell in the section so you only need to test for the section index. Making the text field the first responder will automatically bring up the keyboard.
  */
  override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.section == 0 {
      nameTextField.becomeFirstResponder()
    }
  }
  
  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "SavePlayerDetail" {
      player = Player(name: self.nameTextField.text, game: game, rating: 1)
    }
    if segue.identifier == "PickGame" {
      if let gamePickerViewController = segue.destinationViewController as? GamePickerViewController {
        gamePickerViewController.selectedGame = game
      }
    }
  }
  
  /*
  required init!(coder aDecoder: NSCoder!) {
    println("init PlayerDetailsViewController")
    super.init(coder: aDecoder)
  }
  
  deinit {
    println("deinit PlayerDetailsViewController")
  }
  */
}
